package com.hundsun.yr.universal.p2p.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppCompatActivity;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.p2p.HsApplication;
import com.hundsun.yr.universal.p2p.core.FingerprintCore;
import com.hundsun.yr.universal.p2p.core.KeyguardLockScreenManager;

/**
 * Created by xx on 2017/6/27.
 */
public class FingerprintLockActivity  extends BaseAppCompatActivity implements View.OnClickListener {
    private FingerprintCore mFingerprintCore;
    private KeyguardLockScreenManager mKeyguardLockScreenManager;
    private TextView tv_cancel;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_fingerprintlock;
    }

    @Override
    protected void initViewsAndEvents() {
        mFingerprintCore = new FingerprintCore(this);
        mFingerprintCore.setFingerprintManager(mResultListener);
        mKeyguardLockScreenManager = new KeyguardLockScreenManager(this);
        tv_cancel = findView(R.id.tv_cancel);

        //开始进行指纹识别
        if (mFingerprintCore.isSupport()) {
//            if (!mFingerprintCore.isHasEnrolledFingerprints()) {
//                //还没进行指纹录入
//                FingerprintUtil.openFingerPrintSettingPage(this);
//                return;
//            }

            if (mFingerprintCore.isAuthenticating()) {

            } else {
                mFingerprintCore.startAuthenticate();
            }
        } else {
            //此设备不支持指纹识别
        }

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //不做任何操作
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void resetGuideViewState() {
    }

    private FingerprintCore.IFingerprintResultListener mResultListener = new FingerprintCore.IFingerprintResultListener() {
        @Override
        public void onAuthenticateSuccess() {
            resetGuideViewState();
            showToast("指纹识别成功");
            Intent intent = getIntent();
            setResult(2,intent);
            finish();
        }

        @Override
        public void onAuthenticateFailed(int helpId) {
//            showToast("指纹识别失败");
            showToast("指纹识别错误等待几秒再重试");
        }

        @Override
        public void onAuthenticateError(int errMsgId) {
            resetGuideViewState();

        }

        @Override
        public void onStartAuthenticateResult(boolean isSuccess) {

        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        HsApplication.goFinger = false;
        HsApplication.isSrcreenOn = false;
    }
}
